package com.hxht.cmp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxht.cmp.entity.Log;
import com.hxht.cmp.mapper.LogMapper;
import com.hxht.cmp.pojo.LogInfoRequirement;
import com.hxht.cmp.service.LogInfoService;
import com.hxht.cmp.utils.CourtUidListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogInfoServiceImpl implements LogInfoService {

    @Autowired
    private LogMapper logMapper;


    @Override
    public Map<String, Object> selectLogInfo(LogInfoRequirement logInfoRequirement) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> resultMap = new HashMap<>();
        Example example = new Example(Log.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != logInfoRequirement) {
            if (logInfoRequirement.getStarts().equals("0")) {
                //单法院
                if (!ObjectUtils.isEmpty(logInfoRequirement.getCourtUid())) {
                    criteria.andEqualTo("courtUid", logInfoRequirement.getCourtUid());
                }
            } else if (logInfoRequirement.getStarts().equals("1")) {
                //区域法院
                if (!ObjectUtils.isEmpty(logInfoRequirement.getCourtUid())) {
                    criteria.andIn("courtUid", CourtUidListUtils.getAllCourUidList(logInfoRequirement.getCourtUid()));
                }
            }
            if (!ObjectUtils.isEmpty(logInfoRequirement.getSysName())) {
                criteria.andEqualTo("sysName", logInfoRequirement.getSysName());
            }
            if (!ObjectUtils.isEmpty(logInfoRequirement.getLogType())) {
                criteria.andEqualTo("logType", logInfoRequirement.getLogType());
            }
            if (!ObjectUtils.isEmpty(logInfoRequirement.getInfoType())) {
                criteria.andEqualTo("infoType", logInfoRequirement.getInfoType());
            }
            if (!ObjectUtils.isEmpty(logInfoRequirement.getIp())) {
                criteria.andEqualTo("ip", logInfoRequirement.getIp());
            }
            if (!ObjectUtils.isEmpty(logInfoRequirement.getStartDate())) {
                criteria.andGreaterThanOrEqualTo("dateCreated", logInfoRequirement.getStartDate());
            }
            if (!ObjectUtils.isEmpty(logInfoRequirement.getEndDate())) {
                //结束日期往后推一天
                String endDate = logInfoRequirement.getEndDate();
                criteria.andLessThanOrEqualTo("dateCreated", CaseReviewServiceImpl.addDateOneDay(endDate));
            }
            example.setOrderByClause("date_created desc");
            Page<Log> logPage = PageHelper.startPage(logInfoRequirement.getPage(), logInfoRequirement.getPageSize());
            List<Log> logList = logMapper.selectByExample(example);
            //获取总数
            resultMap.put("count", logPage.getTotal());
            resultMap.put("logList", logList);
            return resultMap;
        } else {
            Page<Log> logPage = PageHelper.startPage(logInfoRequirement.getPage(), logInfoRequirement.getPageSize());
            List<Log> logList = logMapper.selectAll();
//            PageInfo pageInfo = new PageInfo(logList);  //获得其他详细信息
            //获取总数
            resultMap.put("count", logPage.getTotal());
            resultMap.put("logList", logList);
            return resultMap;
        }
    }
}
